module page {

    export class GameOverPage extends egret.Sprite {

        public static readonly BTNS_GRPOP_BOTTOM: number = 30;

        private static sheet: egret.SpriteSheet;

        private static sheetCache: any = {};

        private static playerSheetJson: any;

        readonly closeBtn: ui.PushButton;

        readonly bottomBtnBox: egret.DisplayObjectContainer;

        readonly rankPage: RankPage;

        private bgImg: egret.Bitmap;

        public constructor() {
            super();

            this.bgImg = manager.ResManager.getBitmap("game_sheet.black_mask");
            this.addChild(this.bgImg);
            this.bgImg.alpha = 0.85;
            this.bgImg.touchEnabled = true;
            this.bgImg.width = manager.DisplayManager.STAGE_WIDTH;
            this.bgImg.height = manager.DisplayManager.STAGE_HEIGHT;


            this.bottomBtnBox = new egret.DisplayObjectContainer();
            this.addChild(this.bottomBtnBox);

            this.rankPage = new RankPage();
            this.addChild(this.rankPage);

            this.closeBtn = new ui.PushButton(manager.ResManager.getBitmap("game_sheet.btn_close_panel"));
            this.addChild(this.closeBtn);
            this.closeBtn.x = (manager.DisplayManager.STAGE_WIDTH - this.closeBtn.width) / 2;
            this.closeBtn.y = manager.DisplayManager.STAGE_HEIGHT - this.closeBtn.height - 50;
            this.closeBtn.addEventListener(ui.PushButton.CLICK, this.onCloseBtnClick, this);
            this.visible = false;
        }

        private onVideoAdClose(eve: egret.Event): void {
        }

        public resize(): void {
            let scale: number = this.bottomBtnBox.scaleX;
            this.bottomBtnBox.x = manager.DisplayManager.STAGE_WIDTH
                - this.bottomBtnBox.width * scale >> 1;
            this.bottomBtnBox.y = manager.DisplayManager.GAME_HEIGHT
                - this.bottomBtnBox.height * scale - GameOverPage.BTNS_GRPOP_BOTTOM;
        }

        private onSkip(eve: egret.Event): void {
            this.hideAllChildPage();
            this.bottomBtnBox.visible = true;
            this.rankPage.visible = true;
            let score = manager.GameManager.getInstance().getScore();
            let userSetting: user.UserSetting = manager.GameManager.getInstance().getUserSetting();
            if (userSetting.maxScore < score) {
                this.rankPage.show(RankPage.SUBMIT, this.scoreData);
                userSetting.saveMaxScore(score);
            } else {
                this.rankPage.show(RankPage.SHOW_GAME_OVER_RANK, this.scoreData);
            }
        }

        private showScorePage(): void {

        }

        public show(): void {
            this.visible = true;
        }

        public hide(): void {
            this.visible = false;
        }

        private onCloseBtnClick(eve): void {
            this.hideRank();
        }

        public hideRank(): void {
            this.rankPage.hide();
            this.visible = false;
        }

        private scoreData: Array<any>;


        private hideAllChildPage(): void {
            if (this.rankPage != null) {
                this.rankPage.visible = false;
            }
            this.bottomBtnBox.visible = false;
            this.closeBtn.visible = false;
        }

        public showRank(scoreData: Array<any>, command: number) {
            if (this.visible) {
                return;
            }
            this.visible = true;
            this.hideAllChildPage();
            if (command == RankPage.SUBMIT) {
                this.scoreData = scoreData;
                this.showScorePage();
            } else if (command == RankPage.SHOW) {
                this.closeBtn.visible = true;
                this.rankPage.visible = true;
                this.rankPage.show(command, scoreData);
                egret.log("显示排行榜");
            } else if (command == RankPage.SHOW_GROUP_RANK) {
                this.closeBtn.visible = true;
                this.rankPage.visible = true;
                this.rankPage.show(command, scoreData);
            } else {
                this.visible = false;
            }
        }

        public static getTexture(name: string): egret.Texture {
            if (GameOverPage.sheet == null) {
                GameOverPage.playerSheetJson = RES.getRes("rank_sheet_json");
                let playerTexture: egret.Texture = manager.ResManager.getTexture("rank_sheet_image");
                GameOverPage.sheet = new egret.SpriteSheet(playerTexture);
            }
            if (!GameOverPage.sheetCache.hasOwnProperty(name)) {
                let sheetData: any = GameOverPage.playerSheetJson["frames"];
                sheetData = sheetData[name];
                GameOverPage.sheetCache[name] = 1;
                this.sheet.createTexture(name, sheetData.x, sheetData.y,
                    sheetData.w, sheetData.h);
            }
            return GameOverPage.sheet.getTexture(name);
        }
    }

    export class SubmitScorePostData {
        public token: string = "";
        public score: number = 0;
        public other: number = 1;
    }

}